<?php
// @formatter:off
/**
 * @file WCSession.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: WCSession.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/security/WCSession.php $
 */
// @formatter:on

/**
 * @class WCSession
 */
class WCSession extends WCTableRepresentation {
	protected $_PropertiesReadOnly = array("id");
	protected $_PropertiesPrefix = "sess_";
	protected $_PropertiesTable = "sessions";
	protected $_PropertiesAutoSet = ", sess_timestamp = sysdate()";

	//
	// Public methods
	public function __set($name, $value) {
		global $wcDefaults;
		$ReadOnlyMode = $wcDefaults["read-only"];

		$wcDefaults["read-only"] = false;
		$out = parent::__set($name, $value);

		$wcDefaults["read-only"] = $ReadOnlyMode;

		return $out;
	}
	public function load($id) {
		parent::load($id);

		if($this->ok() && !$this->salt) {
			$this->salt = rand(1, 999999);
		}

		return $this->ok();
	}
	public function setId($id) {
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;

			$query = "update {$this->_dbprefix}{$table}\n";
			$query .= "set    {$prefix}id = :newid\n";
			$query .= "where  {$prefix}id = :currentid\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}

		$params = array(
			":newid" => $id,
			":currentid" => $this->id
		);

		$stmt->execute($params);

		$this->load($id);

		return $this->ok();
	}
	//
	// Protected methods
}
?>